package com.example.login.controller;

import com.example.login.entity.User;
import com.example.login.repository.UserRepository;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Optional;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserRepository userRepository;

    @PostMapping("/login")
    public User login(@RequestBody User user){
        System.out.println(user);
        return userRepository.findByUsernameAndPassword(user.getUsername(), user.getPassword());

    }


    @GetMapping("/{id}")
    public User getUserById(@PathVariable Integer id){
        Optional<User> user = userRepository.findById(id);
        return user.orElseGet(null);
    }


    @PostMapping("/save")   //添加信息
    public User insertUser(@RequestBody User user1) {
        User user = new User();
        user.setUsername(user1.getUsername());
        user.setPassword(user1.getPassword());
        user.setNickname(user1.getNickname());
        user.setAddress(user1.getAddress());
        user.setBalance(user1.getBalance());

        user = userRepository.save(user);
        return user;
    }

}
